package com.example.boot.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.example.boot.entity.User;
import com.example.boot.mapper.UserMapper;
import com.example.boot.service.UserService;
import com.lemon.core.mybatis.base.service.SuperServiceImpl;
import com.lemon.core.redis.core.service.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author pan
 * @since 2022-03-22
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements UserService {

    private final RedisService redisService;

    @Override
    public User getUserByName(String username) {
        Object o = redisService.get(username);
        if (Objects.nonNull(o)) {
            return (User) o;
        }
        User user = new User(IdUtil.getSnowflakeNextId(), username, RandomUtil.randomString(6));
        redisService.set(username, user);
        log.info("create user: {}", user);
        return user;
    }
}
